import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {SetResponseNormal} from "./SetResponseNormal";
import {SetResponseNormalList} from "./SetResponseNormalList";
import {SetThenGetResponseNormalList} from "./SetThenGetResponseNormalList";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";


export enum SetResponseChoice {
    _ERR_NONE_SELECTED = -1,
    SET_RESPONSE_NORMAL = 1,
    SET_RESPONSE_NORMAL_LIST = 2,
    SET_THEN_GET_RESPONSE_NORMAL_LIST = 3,
}

export class Set_Response implements AxdrType{

    dataCode : Buffer | null = null;
    choice : SetResponseChoice = SetResponseChoice._ERR_NONE_SELECTED;
    setResponseNormal : SetResponseNormal | null = null;
    setResponseNormalList : SetResponseNormalList | null = null;
    setThenGetResponseNormalList : SetThenGetResponseNormalList | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setSetResponseNormal(setResponseNormal : SetResponseNormal){
        this.resetChoices();
        this.choice = SetResponseChoice.SET_RESPONSE_NORMAL;
        this.setResponseNormal = setResponseNormal;
    }

    setSetResponseNormalList(setResponseNormalList : SetResponseNormalList){
        this.resetChoices();
        this.choice = SetResponseChoice.SET_RESPONSE_NORMAL_LIST;
        this.setResponseNormalList = setResponseNormalList;
    }

    setSetThenGetResponseNormalList(setThenGetResponseNormalList : SetThenGetResponseNormalList){
        this.resetChoices();
        this.choice = SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST;
        this.setThenGetResponseNormalList = setThenGetResponseNormalList;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL) {
            this.setResponseNormal = new SetResponseNormal();
            codeLength += this.setResponseNormal.decode(input);
            return codeLength;
        }else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL_LIST) {
            this.setResponseNormalList = new SetResponseNormalList();
            codeLength += this.setResponseNormalList.decode(input);
            return codeLength;
        }else if (this.choice == SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST) {
            this.setThenGetResponseNormalList = new SetThenGetResponseNormalList();
            codeLength += this.setThenGetResponseNormalList.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == SetResponseChoice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST) {
            if(this.setThenGetResponseNormalList != null){
                codeLength += this.setThenGetResponseNormalList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(3);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("setThenGetResponseNormalList is null");
            }
        }else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL_LIST) {
            if(this.setResponseNormalList != null){
                codeLength += this.setResponseNormalList.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(2);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("setResponseNormalList is null");
            }
        }else if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL) {
            if(this.setResponseNormal){
                codeLength += this.setResponseNormal.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("setResponseNormal is null");
            }
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    resetChoices() {
        this.choice = SetResponseChoice._ERR_NONE_SELECTED;
        this.setResponseNormal = null;
        this.setResponseNormalList = null;
        this.setThenGetResponseNormalList = null
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): SetResponseChoice {
        return this.choice;
    }

    toString(): string {
        if (this.choice == SetResponseChoice.SET_RESPONSE_NORMAL) {
            if (this.setResponseNormal != null) {
                return "choice: {setResponseNormal: " + this.setResponseNormal + "}";
            } else {
                return "choice is setResponseNormal but setResponseNormal is null";
            }
        }else if(this.choice == SetResponseChoice.SET_RESPONSE_NORMAL_LIST){
            if(this.setResponseNormalList != null){
                return "choice: {setResponseNormalList: " + this.setResponseNormalList + "}";
            }else{
                return "choice is setResponseNormalList but setResponseNormalList is null";
            }
        }else if(this.choice == SetResponseChoice.SET_THEN_GET_RESPONSE_NORMAL_LIST){
            if(this.setThenGetResponseNormalList != null){
                return "choice: {setThenGetResponseNormalList: " + this.setThenGetResponseNormalList + "}";
            }else{
                return "choice is setThenGetResponseNormalList but setThenGetResponseNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

}
